//
//  ChoiceViewController.swift
//  cxd4iphone
//
//  Created by hexy on 15/11/19.
//  Copyright © 2015年 hexy. All rights reserved.
//

import UIKit
import SnapKit
import FDFullscreenPopGesture
import MJRefresh

private struct kConstraints {
    
    static let headerRect: CGRect = CGRectMake(0, 0, xx_screenWidth(), xx_height(150))
    static let footerRect: CGRect = CGRectMake(0, 0, xx_screenWidth(), xx_screenHeight()-49-xx_height(150))
}

class ChoiceViewController: BaseTableViewController {
    
    private lazy var choiceListViewModel = ChoiceListViewModel()
    lazy var wd = UIWindow(frame: CGRectMake(0, 0, xx_screenWidth(), 20))
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.fd_prefersNavigationBarHidden = true
        
        tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: { () -> Void in
            
            self.loadData()
        })
        tableView.mj_header.beginRefreshing()
        
        APIManager.queryBankCard().subscribeNext({ (result) -> Void in
            
                xx_JsonPrint(dict: result)
            }, error: { (error) -> Void in
                
                xx_print(error)
            }, completed: {})
        
//
//        APIManager.loadQueryData().subscribeNext({ (result) -> Void in
//            
//                xx_JsonPrint(dict: result)
//            
//            }, error: { (error) -> Void in
//                
//                xx_print(error)
//            }, completed: {})
        
//        CGRectMake(0, 0, xx_screenWidth(), 20)
//      let wd = UIWindow(frame: CGRectMake(0, 0, xx_screenWidth(), 20))
//        wd.backgroundColor = UIColor.orangeColor()
//        wd.windowLevel = UIWindowLevelNormal
//        wd.hidden = false
        
//        AccountViewModel.shared.checkUserName("hexyfdscc1909023").subscribeError({ (error) -> Void in
//            
//            xx_showError(error)
//            xx_print(error)
//            }) { () -> Void in
//                
////                xx_print(AccountViewModel.shared.userAuthen)
//        }

     
//        AccountViewModel.shared.checkAuthentication().subscribeError({ (error) -> Void in
//            
//                xx_showError(error)
//            }) { () -> Void in
//                
//                xx_print(AccountViewModel.shared.userAuthen)
//        }
//        
//        APIManager.checkIsUsedPhone("15311155959").subscribeNext({ (result) -> Void in
//            
//                xx_JsonPrint(dict: result)
//            }, error: { (error) -> Void in
//                
//                xx_showError(error)
//            }, completed: {})
//        xx_print(AccountViewModel.shared.userLogin)
//        AccountViewModel.shared.login("fjsklfjd9090l", passwd: "123aaa").subscribeError({ (error) -> Void in
//            
//            xx_showError(error)
//            xx_print(error)
//            }, completed: {})
        
        
        
//                APIManager.verifiCode(po).subscribeNext({ (result) -> Void in
//        
//        
//        //                xx_JsonPrint(dict: result)
//                    let code = (result as! [String : AnyObject])["authCode"] as! String
//                    xx_print(code)
//        
//                    let mo =  AccountViewModel.shared.registModel
//                    mo.phoneNo = po
//                    mo.userName = ue
//                    mo.password = "123aaa"
//                    mo.authCode = code
//        
//                    AccountViewModel.shared.register().subscribeNext({ (result) -> Void in
//        
//                            xx_JsonPrint(dict: result)
//        
//                     
//                        
//        
//                        }, error: { (error) -> Void in
//                            
//                            xx_print(error)
//                        }, completed: {})
//                    
//                    }, error: { (error) -> Void in
//                        
//                        xx_print(error)
//                    }, completed: {})


//
        
//       YPRequestViewModel.shared.bindBankCard().subscribeError({ (error) -> Void in
//        
//            xx_print(error)
//        }) { () -> Void in
//            
//            let vc = WebViewController(htmlStr: YPRequestViewModel.shared.html)
//            vc.title = "绑卡"
//            self.navigationController?.pushViewController(vc, animated: true)
//        }

//        
//               YPRequestViewModel.shared.changeTradePwd().subscribeError({ (error) -> Void in
//        
//                    xx_print(error)
//                }) { () -> Void in
//        
//                    let vc = WebViewController(title: "修改交易密码", htmlStr: YPRequestViewModel.shared.html)
//                    
//                    self.navigationController?.pushViewController(vc, animated: true)
//                }

    }
    
    func loadData() {
        
        choiceListViewModel.loadBanner().subscribeError({ (error) -> Void in
            
            xx_print(error)
            
            }) { () -> Void in
                
                self.tableView.mj_header.endRefreshing()
                self.carousel.setImages(self.choiceListViewModel.imageURLs, timeInterval: nil, clickImageBlock: { (index) -> () in
                    
                    self.pushToCarouselDetail(index)
                })
        }
        
        
        choiceListViewModel.loadNewerBid().subscribeNext({ (_) -> Void in
            
            }, error: { (error) -> Void in
                
                xx_print(error)
            }) { () -> Void in
                
                self.tableView.mj_header.endRefreshing()
                
                self.recommend.bidViewModel = self.choiceListViewModel.bidViewModel
        }
        
        PickerQueryViewModel.shared.loadPickerQueryData().subscribeError({ (error) -> Void in
            
            xx_print(error)
            }) { () -> Void in
                
                xx_print(PickerQueryViewModel.shared.billDateQuery)
        }
    }
    
    override func makeTableView() {
        
        self.tableListViewModel = choiceListViewModel
        
        automaticallyAdjustsScrollViewInsets = false
        tableView.backgroundColor = UIColor.whiteColor()
        tableView.tableHeaderView = carousel
        tableView.tableFooterView = recommend
        
    }
    
    
    
    func pushToCarouselDetail(index: Int) {
        
        let URLStr = choiceListViewModel.carousels[index].carouselModel.url!
//        let URLStr = "http://www.baidu.com"
        let title = choiceListViewModel.carousels[index].carouselModel.title!

        navigationController?.pushViewController(WebViewController(title: title, URLStr: URLStr), animated: true)
    }
    
    private lazy var carousel = CarouselView(frame: kConstraints.headerRect)
    
    private lazy var recommend: ChoiceFooterView = {
        
        let recommend = ChoiceFooterView(frame: kConstraints.footerRect)
        
        recommend.profitBtn.rac_signalForControlEvents(.TouchUpInside).subscribeNext { (_) -> Void in
            
            self.navigationController?.pushViewController(BuyViewController(bidID: self.choiceListViewModel.bidId), animated: true)
            
        }
        return recommend
    }()
    
}
